package connecter
import java.sql.{DriverManager, Connection}


class MySQL {
  private val driver = "com.mysql.jdbc.Driver"
  private val url = "jdbc:MySQL://localhost/work"
  private val username = "root"
  private val password = "123456"

  Class.forName(driver)  // 注册
  private val conn: Connection = DriverManager.getConnection(url, username, password)
  private val cursor = conn.createStatement()

  def getAllFields() {
    // 获得所有行
    try {
      val resultSet = cursor.executeQuery("SELECT * FROM t_province;")
      while ( resultSet.next() ) {
        val name = resultSet.getString("name")
        val pro_sort = resultSet.getString("pro_sort")
        println("name, pro_sort = " + name + ", " + pro_sort)
      }
    } catch {
      case e => e.printStackTrace
    }
    conn.close()
  }

  def getStructor() {
    // 获得表结构
    val res = cursor.executeQuery("SHOW CREATE TABLE t_province;")
    while (res.next()) {
      val title = res.getString("Table")
      val content = res.getString("Create Table")
      println(title)
      println(content)
    }
    conn.close()
  }
}


object Runner {

  def main(args: Array[String]) {
    val obj: MySQL = new MySQL()
//    obj.getAllFields()
    obj.getStructor()
  }
}